package com.sicau.smart_farmer.service;

import com.sicau.smart_farmer.entity.TrainingBatch;
import com.sicau.smart_farmer.repository.TrainingBatchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainingBatchService {


    @Autowired
    private TrainingBatchRepository trainingBatchRepository;

    /**
     * 新增培训批次
     * @param trainingBatch
     * @return
     */
    public boolean addTrainingBatch(TrainingBatch trainingBatch){
        TrainingBatch save = trainingBatchRepository.save(trainingBatch);
        return save != null;
    }

    /**
     * 根据id更新培训批次
     * @param trainingBatch
     * @return
     */
    public TrainingBatch updateTrainingBatch(TrainingBatch trainingBatch) {
        if (!trainingBatchRepository.findById(trainingBatch.getId()).isPresent()){
            return null;
        }
        return trainingBatchRepository.save(trainingBatch);
    }

    /**
     * 根据批次id删除
     * @param trainingBatchId
     */
    public void deleteTrainingBath(String trainingBatchId){
        trainingBatchRepository.deleteById(trainingBatchId);
    }

    /**
     * 获取所有的批次
     * @return
     */
    public List<TrainingBatch> getAllTrainingBatch(){
        List<TrainingBatch> all = trainingBatchRepository.findAll();
        return all;
    }


    /**
     * 通过id查询相应的培训批次
     * @param id
     * @return
     */
    public TrainingBatch queryTranningBatchById(String id){
        TrainingBatch trainingBatchById = trainingBatchRepository.findTrainingBatchById(id);
        return trainingBatchById;
    }

}
